﻿namespace QMVnSoft.DAL
{
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class CDVTDataAccess
    {
        public bool Delete(object po_KeyCode)
        {
            bool flag;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt0", po_KeyCode), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[1].Direction = ParameterDirection.Output;
            values[1].SqlDbType = SqlDbType.Int;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CdNTXT_Delete";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                AppDataAccess.CloseConnection(eZConnection);
                flag = ((int) values[1].Value) == 1;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            return flag;
        }

        public DataTable GetDataTable(string ps_Subsidiary)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary) };
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            DataTable dataTable = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CdNTXT_Kho_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                AppDataAccess.CloseConnection(eZConnection);
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return table2;
        }

        public DataTable GetDataTable(string ps_Subsidiary, object ps_Code)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ma_Kho", ps_Code) };
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            DataTable dataTable = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CdNTXT_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                AppDataAccess.CloseConnection(eZConnection);
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return table2;
        }

        public int Save(bool pb_NewChecked, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { 
                new SqlParameter("p_Moi_Sua", pb_NewChecked), new SqlParameter("p_Stt0", row["Stt0"]), new SqlParameter("p_Ma_DvCs", row["Ma_DvCs"]), new SqlParameter("p_Ma_Kho", row["Ma_Kho"]), new SqlParameter("p_Ma_Vt", row["Ma_Vt"]), new SqlParameter("p_Dvt", row["Dvt"]), new SqlParameter("p_He_So9", row["He_So9"]), new SqlParameter("p_So_Luong9", row["So_Luong9"]), new SqlParameter("p_So_Luong", row["So_Luong"]), new SqlParameter("p_Tien", row["Tien"]), new SqlParameter("p_Lo_Hang", row["Lo_Hang"]), new SqlParameter("p_Han_Dung", row["Han_Dung"]), new SqlParameter("p_H_Luong", row["H_Luong"]), new SqlParameter("p_Do_Am_PT", row["Do_Am_PT"]), new SqlParameter("p_Do_Con_PT", row["Do_Con_PT"]), new SqlParameter("p_Ngay_Ct", row["Ngay_Ct"]), 
                new SqlParameter("p_Result", SqlDbType.BigInt)
             };
            values[0x10].Direction = ParameterDirection.Output;
            values[0x10].SqlDbType = SqlDbType.Int;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CdNTXT_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                AppDataAccess.CloseConnection(eZConnection);
                num = (int) values[0x10].Value;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }
    }
}

